//
//  HomeVC.m
//  PersonalOC
//
//  Created by 韩琴 on 2018/8/17.
//  Copyright © 2018年 韩琴. All rights reserved.
//

#import "HomeVC.h"
#import "SearchVC.h"
#import "HomeTableViewCell.h"
#import "HomeModle.h"
#import "HomeScanVC.h"

@interface HomeVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)BaseTableView *mainTableView;
@property (nonatomic,strong)UIView *searchView;
@property (nonatomic,strong)HQCustomDialogs *animatorManager;
@end

@implementation HomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)viewDidAppear:(BOOL)animated {
//    NSLog(@"%f%f",self.mainTableView.frame.size.height,self.mainTableView.contentSize.height);
}
//懒加载
- (HQCustomDialogs *)animatorManager {
    if (!_animatorManager) {
        HQCustomDialogs *manager = [[HQCustomDialogs alloc] init];
        manager.presentFrame = CGRectMake(LY_M_SCREEN_W-116, LY_navgationBarAndStatusBar_Height, 100, 44*2);
        manager.prsentDeriction = HQPresentDerictionTop;
        manager.backGrougdColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        manager.isCoverEnlable = true;
        _animatorManager = manager;
    }
    
    return _animatorManager;
}
-(void)loadUI {
    //设置内容相对(让内容计较navgationbar)
    self.edgesForExtendedLayout = UIRectEdgeTop;
    
    //TableView
    BaseTableView *maintabel = [[BaseTableView alloc] init];
    [self.view addSubview:maintabel];
    self.mainTableView = maintabel;
    [maintabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(@0);
    }];
    maintabel.delegate = self;
    maintabel.dataSource = self;
    maintabel.separatorStyle = UITableViewCellSeparatorStyleNone;
    maintabel.estimatedSectionFooterHeight = 0;
    //bannar和菜单
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LY_M_SCREEN_W, LY_M_SCREEN_W*0.7)];
    maintabel.tableHeaderView = topView;
    XRCarouselView *carouseView = [LYView addBannerViewWithUrlArr:@[@"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1368076744,3453938551&fm=27&gp=0.jpg",@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3065998735,4285929137&fm=200&gp=0.jpg"] withSuperView:topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-80);
    } withDidImageClickBlock:^(NSInteger index) {
        NSLog(@"%ld",index);
    }];
    UIView *topMenuView = [UIView hyb_viewWithSuperView:topView constraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.equalTo(carouseView.mas_bottom);
    }];
    NSArray *titleArr = @[@"高薪职位",@"名企专区",@"附近职位",@"竞争力"];
    NSArray *imgArr   = @[@"高薪职位",@"名企专区",@"附近职位",@"竞争力"];
    CGFloat btnW      = LY_M_SCREEN_W / titleArr.count;
    for (int x= 0 ; x<titleArr.count; x++) {
        QMUIButton *btn = [[QMUIButton alloc] qmui_initWithImage:LY_IMAGE_NAMED(imgArr[x]) title:titleArr[x]];
        btn.spacingBetweenImageAndTitle = 2;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [topMenuView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(x*btnW);
            make.bottom.top.mas_equalTo(0);
            make.width.mas_equalTo(btnW);
        }];
        
        btn.imagePosition =  QMUIButtonImagePositionTop;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    
    //导航栏搜索框
    UIView *allView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, LY_M_SCREEN_W-88, 30)];
    [LYTools viewAddFillet:allView];
    allView.alpha = 0.8;
    self.searchView = allView;
    self.navigationItem.titleView = self.searchView;
    [LYTools viewFillet:allView andFloat:15];
    allView.backgroundColor = UIColor.whiteColor;
    [self setupNav];
    [self registerCell];
}
//设置导航栏
- (void)setupNav{
    //去掉导航栏分割线
    [self ly_setNavBarShadowImageHidden:YES];
    [self ly_setNavBarBackgroundAlpha:0];
    
    QMUIButton *btn = [[QMUIButton alloc] qmui_initWithImage:LY_IMAGE_NAMED(@"扫一扫") title:@"扫一扫"];
    btn.spacingBetweenImageAndTitle = 2;
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.imagePosition =  QMUIButtonImagePositionTop;
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    btn.frame = CGRectMake(0, 0, 40, 35);
    [btn addTarget:self action:@selector(didClicsaoYisao:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)registerCell {
    [self.mainTableView registerClass:HomeTableViewCell.self forCellReuseIdentifier:@"HomeTableViewCell"];
}
//点击了扫一扫
- (void)didClicsaoYisao:(QMUIButton *)sender {
    NSLog(@"%@",sender);
//    [LYMedia OpenCodeVcWithCuVc:self withResultBlock:^(NSString *result) {
//        NSLog(@"%@",result);
//    }];
    HomeScanVC *toolsView = [[HomeScanVC alloc] init];
    toolsView.view.backgroundColor = UIColor.whiteColor;
    toolsView.transitioningDelegate = self.animatorManager;
    toolsView.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:toolsView animated:true completion:nil];
}

//MARK：- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTableViewCell" forIndexPath:indexPath];
    
    HomeModle *modle = [[HomeModle alloc] init];
    modle.title = @"IOS工程师";
    modle.time = @"3年";
    modle.address = @"重庆";
    modle.tagsArrary = @[@" 双休 ",@" 年底双薪 ",@" 五险一金 "];
    modle.edu = @"本科";
    modle.money = @"1万-1.5万/月";
    modle.status = @"8月20日";
    modle.company = @"重庆网络科技公司";
    cell.cellModle = modle;
    
    return  cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 98;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self ly_setNavBarBackgroundAlpha:scrollView.contentOffset.y/100.0];

    if(scrollView.contentOffset.y<0)
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.searchView.alpha = 0;
        }];
    }
    else
    {
        [UIView animateWithDuration:0.2 animations:^{
            self.searchView.alpha = 0.8;
        }];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [UIView new];
    headerView.backgroundColor = UIColor.groupTableViewBackgroundColor;
    return headerView;
}
@end
